import {
	tabBarIndex,
	noReadNumName,
	chatListName,
	sendOneType,
	sendCircleType,
	textType,
	imgType,
	voiceType
} from '@/config/config.js'
import localStore from '@/helpers/localStore.js'
import Time from '@/helpers/time.js'

export function chatFormat(res, options = { isCircle: false }, data) {
	const { isCircle } = options
	switch (options.type) {
		case 'chatList':
			if (isCircle) {
				let obj = {
					circleId: res.body.circleId,
					circleName: res.body.circleName,
					avatar: res.body.circleImg,
					circleType: res.body?.circleType,
					time: res.body.createTime,
					data: formatMsg(res.body.type, {
						username: res.body?.username,
						content: res.body.content,
						isCircle
					}),
					noReadNum: 0 // 未读数
				}
				// 本人发送的信息
				if (res.body.userid == data.userid) {
					obj.data = formatMsg(res.body.type, {
						content: res.body.content,
						isCircle,
						isMe: true
					})
				}
				return obj
			}

			let obj = {
				userid: res.body.userid,
				username: res.body.username,
				avatar: res.body.userImg,
				time: res.body.createTime,
				data: formatMsg(res.body.type, { content: res.body.content }),
				noReadNum: 0 // 未读数
			}
			// 本人发送的信息
			if (res.body.userid == data.userid) {
				obj.userid = data.toUser
				obj.username = data.toUserName
				obj.avatar = data.toUserAvatar
			}
			return obj

		case 'chatDetail':
			const list = options.oldData || []
			const lastTime =
				list?.length > 0
					? [...list].reverse().find((i) => i.msg.time).msg.time
					: 0
			let lastId = list?.length > 0 ? list[list.length - 1].msg.id : -1
			lastId++
			return {
				type: 'user',
				msg: {
					id: lastId,
					type: res.body.type,
					showTime: Time.noFormatChatTime(res.body.createTime, lastTime),
					time: res.body.createTime,
					userinfo: {
						uid: res.body.userid,
						username: isCircle ? res.body.username : '',
						face: res.body.userImg
					},
					content: res.body.content
				}
			}

		case 'send':
			if (isCircle) {
				return {
					type: sendCircleType,
					body: {
						type: res.msg.type,
						circleId: data.circleId,
						circleName: data.circleName,
						circleImg: data.circleAvatar,
						circleType: data.circleType,
						userid: data.userid,
						userImg: data.avatar,
						content: res.msg.content,
						createTime: new Date().getTime()
					}
				}
			}
			return {
				type: res.type,
				body: {
					type: res.msg.type,
					userid: data.userid,
					username: data.username,
					userImg: data.avatar,
					toUser: data.toUser,
					message: res.msg.message,
					createTime: new Date().getTime()
				}
			}
	}
}

// 格式化历史聊天列表
export function chatListFormat(list) {

	if (list.length === 0) return []
	let chatList = []
	list.map((i) => {
			chatList.push({
				userid: i.receiverid,
				username: i.uname,
				avatar: i.avatar,
				data: i.message,
				time: i.sendtime,
				noReadNum: i.is_read
			})
		})

		// alert(chatList[0].time) //20:49:13
	return chatList.sort((a, b) => b.time - a.time)
}

// 格式化历史聊天消息
export function chatDetailFormat(list) {

	if (list.length === 0) return []
	let chatDetail = []
	list.map((i) => {
		chatDetail.push({
			type: 'user',
			msg: {
				id: i.id,
				type: i.type || 'text',
				showTime: i.sendTime,
				time: i.sendTime,
				userinfo: {
					uid: i.user.userid,
					username: i.user.uname,
					face: i.user.avatar
				},
				content: i.message
			}
		})
	})

	return chatDetail.sort((a, b) => a.msg.time - b.msg.time)
}

// 读取当前会话
export function readMsg(id) {

	let chatList = localStore.get(chatListName) || []
	const index = chatList?.findIndex((i) => i.userid === id || i.circleId === id)

	// 如果会话存在
	if (index !== -1) {
		const oldNoReadNum = chatList[index].noReadNum
		chatList[index].noReadNum = 0
		localStore.set(chatListName, chatList)
		updateNoReadNum({ type: 'read', num: oldNoReadNum })
	}
}

// 总未读数+1，修改tabBar信息数
export function updateNoReadNum(options = {}) {
	let noReadNum = localStore.get(noReadNumName)
	if (options.type == 'add') {
		noReadNum++
		// this.__Notify()
	} else {
		noReadNum -= options.num
	}
	noReadNum = noReadNum || 0
	updateTabBarBadge(noReadNum)
	localStore.set(noReadNumName, noReadNum)
}

export function initTabBarBadge() {
	const noReadNum = localStore.get(noReadNumName)
	noReadNum && updateTabBarBadge(noReadNum)
}

function updateTabBarBadge(num) {
	if (num > 0) {
		uni.setTabBarBadge({
			index: tabBarIndex,
			text: num <= 99 ? num.toString() : '99+'
		})
	} else {
		uni.removeTabBarBadge({
			index: tabBarIndex
		})
	}
}

export function formatMsg(type, options = { isCircle: false, isMe: false }) {

	let data
	if (type === textType) {

		data = options.message.text.replace(/\n/g, ' ')
	} else if (type === imgType) {
		alert(234)
		data = '[图片]'
	} else if (type === voiceType) {
		alert(345)
		data = '[语音]'
	}

	if (options.isCircle && !options.isMe) data = options.username + '：' + data
	return data
}
